package com.yang.controller;

import com.yang.anno.Log;
import com.yang.pojo.Dept;
import com.yang.pojo.Result;
import com.yang.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 一个完整的请求路径 应该是类上的@RequestMapping的value数据性 + 方法上的@RequestMapping的value属性
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

//    private static Logger log = LoggerFactory.getLogger(DeptController.class);

    @Resource(name = "deptServiceImpl")
    private DeptService deptService;

    /**
     * 查询全部部门数据
     * @return
     */
//    @RequestMapping(value = "/depts",method = RequestMethod.GET)  //指定请求方式为GET
//    @GetMapping("/depts") //指定请求方式为GET

    @Log
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }


    /**
     * 根据id删除部门数据
     * @param id
     */
//    @DeleteMapping("/depts/{id}")
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除部门:{}",id);
        deptService.delete(id);
        return Result.success();
    }


    /**
     * 新增部门
     * @param dept
     * @return
     */
//    @PostMapping("/depts")
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门:{}", dept);
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @Log
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据ID查询,ID:{}", id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门数据
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门数据:{}", dept);
        deptService.update(dept);

        return Result.success();
    }

}
